package com.studyroom.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.studyroom.entity.Feedback;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Result;
import java.time.LocalDateTime;

@Mapper
public interface FeedbackMapper extends BaseMapper<Feedback> {

    @Select("<script>" +
            "SELECT f.*, u.username, " +
            "DATE_FORMAT(f.created_at, '%Y-%m-%d %H:%i:%s') as createdAt, " +
            "DATE_FORMAT(f.updated_at, '%Y-%m-%d %H:%i:%s') as updatedAt " +
            "FROM feedbacks f " +
            "LEFT JOIN users u ON f.user_id = u.id " +
            "WHERE 1=1 " +
            "<if test='keyword != null and keyword != \"\"'>" +
            "  AND (f.content LIKE CONCAT('%', #{keyword}, '%') " +
            "       OR u.username LIKE CONCAT('%', #{keyword}, '%')) " +
            "</if>" +
            "<if test='status != null'>" +
            "  AND f.status = #{status} " +
            "</if>" +
            "<if test='startTime != null'>" +
            "  AND f.created_at &gt;= #{startTime} " +
            "</if>" +
            "<if test='endTime != null'>" +
            "  AND f.created_at &lt;= #{endTime} " +
            "</if>" +
            "ORDER BY f.created_at DESC" +
            "</script>")
    @Results({
        @Result(column = "id", property = "id"),
        @Result(column = "user_id", property = "userId"),
        @Result(column = "content", property = "content"),
        @Result(column = "images", property = "images"),
        @Result(column = "reply", property = "reply"),
        @Result(column = "status", property = "status"),
        @Result(column = "createdAt", property = "createdAt"),
        @Result(column = "updatedAt", property = "updatedAt"),
        @Result(column = "username", property = "username")
    })
    Page<Feedback> searchFeedbacks(
        Page<Feedback> page,
        @Param("keyword") String keyword,
        @Param("status") Integer status,
        @Param("startTime") LocalDateTime startTime,
        @Param("endTime") LocalDateTime endTime
    );

    @Select("<script>" +
            "SELECT f.*, u.username, " +
            "DATE_FORMAT(f.created_at, '%Y-%m-%d %H:%i:%s') as createdAt, " +
            "DATE_FORMAT(f.updated_at, '%Y-%m-%d %H:%i:%s') as updatedAt " +
            "FROM feedbacks f " +
            "LEFT JOIN users u ON f.user_id = u.id " +
            "WHERE 1=1 " +
            "<if test='content != null and content != \"\"'>" +
            "  AND f.content LIKE CONCAT('%', #{content}, '%')" +
            "</if>" +
            "<if test='status != null'>" +
            "  AND f.status = #{status}" +
            "</if>" +
            "ORDER BY f.created_at DESC" +
            "</script>")
    @Results({
        @Result(column = "createdAt", property = "createdAt"),
        @Result(column = "updatedAt", property = "updatedAt"),
        @Result(column = "username", property = "username")
    })
    Page<Feedback> selectFeedbackPage(Page<Feedback> page, 
                                   @Param("content") String content,
                                   @Param("status") Integer status);
} 